package com.itye.deom1.view;

import com.itye.deom1.ClassRoom;
import com.itye.deom1.Group;
import com.itye.deom1.Student;
import com.itye.deom1.until.Constant;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Enumeration;

public class ChangeClassPanel extends JPanel {
    JLabel infoLbl = new JLabel();

    public ChangeClassPanel(MainFrame mainFrame) {
        this.setBorder(new TitledBorder(new EtchedBorder(), "选择班级"));
        this.setLayout(new BorderLayout());  // 使用BorderLayout布局
        
        // 创建一个面板来包含单选按钮，使用垂直BoxLayout
        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
        radioPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));  // 添加边距
        
        ButtonGroup btnGroup = new ButtonGroup();
        
        // 创建七班选项
        JRadioButton sevenClass = new JRadioButton("七班");
        sevenClass.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        sevenClass.setAlignmentX(Component.LEFT_ALIGNMENT);  // 左对齐
        btnGroup.add(sevenClass);
        radioPanel.add(sevenClass);
        radioPanel.add(Box.createVerticalStrut(10));  // 添加垂直间距
        
        // 创建八班选项
        JRadioButton eightClass = new JRadioButton("八班");
        eightClass.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        eightClass.setAlignmentX(Component.LEFT_ALIGNMENT);  // 左对齐
        btnGroup.add(eightClass);
        radioPanel.add(eightClass);
        
        // 添加确认按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 20, 20));
        JButton btnChooseClass = new JButton("确认选择班级");
        btnChooseClass.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        buttonPanel.add(btnChooseClass);
        
        // 创建一个容器面板来包含radioPanel和buttonPanel
        JPanel containerPanel = new JPanel();
        containerPanel.setLayout(new BorderLayout());
        containerPanel.add(radioPanel, BorderLayout.CENTER);
        containerPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        // 将容器面板添加到主面板
        this.add(containerPanel, BorderLayout.NORTH);
        
        btnChooseClass.addActionListener(e -> {
            Enumeration<AbstractButton> elements = btnGroup.getElements();
            boolean isSelected = false;
            while (elements.hasMoreElements()) {
                JRadioButton btn = (JRadioButton) elements.nextElement();
                if (btn.isSelected()) {
                    isSelected = true;
                    mainFrame.setTitle(btn.getText());
                    Constant.CLASS_PATH = btn.getText();
                    infoLbl.setText("班级：" + btn.getText() + "，班级学生总数：" + Constant.students.size());
                    break;
                }
            }
            if (!isSelected) {
                JOptionPane.showMessageDialog(this, "请先选择班级", "", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "选择班级成功", "", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 添加信息标签
        infoLbl.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        infoLbl.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
        this.add(infoLbl, BorderLayout.CENTER);
    }
}